package com.efast.cafe.portal.controller.doc;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.doc.DocGroupUserAuthFormBean;
import com.efast.cafe.portal.bean.doc.DocGroupUserAuthQueryBean;
import com.efast.cafe.portal.service.doc.IDocAuthService;
import com.efast.cafe.util.StringUtil;

@Controller
@RequestMapping("/doc")
public class DocAuthController  extends  AbstractController{
	
	@Autowired
	private IDocAuthService docAuthService;
	
	/**
	 * 查询组下的授权用户以及授权权限
	 * @param userinfo
	 * @return
	 */
	
	@RequestMapping(value="/auth/list")
	public @ResponseBody ResultBean queryGroupUserAuthList(@RequestBody DocGroupUserAuthQueryBean queryBean){
		ResultBean result = new ResultBean(true,null);
		try {
			if(!StringUtil.isNullOrBlank(queryBean.getId())){
			queryBean.setGroupId(Integer.parseInt(queryBean.getId().split("_")[1]));}
			result.setObject(docAuthService.queryGroupUserAuthList(queryBean));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	
	/**
	 * 查询所有文档操作权限 对用户存在的权限标识isChecked为1
	 * @param companyCode
	 * @param username
	 * @param groupId
	 * @return
	 */

	@RequestMapping(value="/auth/detail")
	public @ResponseBody ResultBean queryUserAuthDetail(@RequestParam Integer groupId){
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(docAuthService.queryUserAuthDetail(groupId));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	/**
	 * 清除一个用户在一个组中的授权
	 * @param companyCode
	 * @param username
	 * @param groupId
	 */
	
	@RequestMapping(value="/auth/delete")
	public @ResponseBody ResultBean deleteUserAuth(@RequestBody DocGroupUserAuthFormBean bean){
		ResultBean result = new ResultBean(true,null);
		try {
			List<String> list=new ArrayList<>();
			list.add(bean.getUsername());
			if(!StringUtil.isNullOrBlank(bean.getId())){
				bean.setGroupId(Integer.parseInt(bean.getId().split("_")[1]));}
			docAuthService.deleteUserAuth(list,bean.getGroupId());
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	
	/**
	 * 保存用户文档权限
	 * @param bean
	 */
	
	@RequestMapping(value="/auth/add")
	public @ResponseBody ResultBean saveUserAuth(@RequestBody DocGroupUserAuthFormBean bean){
		ResultBean result = new ResultBean(true,null);
		try {
			if(!StringUtil.isNullOrBlank(bean.getId())){
			bean.setGroupId(Integer.parseInt(bean.getId().split("_")[1]));}
			docAuthService.saveUserAuth(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	/**
	 * 查询文档权限列表
	 */
	@RequestMapping(value="/fileAuth/list")
	public @ResponseBody ResultBean queryFileAuthList(){
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(docAuthService.queryDocPurview("FILE"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	
	/**
	 * 查询文件夹权限列表
	 */
	@RequestMapping(value="/foldAuth/list")
	public @ResponseBody ResultBean queryFoldAuthList(){
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(docAuthService.queryDocPurview("FOLDER"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		
		return result;
	}
	
	/**
	 * 修改一个用户在一个组中的授权
	 * @param companyCode
	 * @param username
	 * @param groupId
	 */
	
	@RequestMapping(value="/auth/updateByUser")
	public @ResponseBody ResultBean updateByUser(@RequestBody DocGroupUserAuthFormBean bean){
		ResultBean result = new ResultBean(true,null);
		try {
			if(!StringUtil.isNullOrBlank(bean.getId())){
			bean.setGroupId(Integer.parseInt(bean.getId().split("_")[1]));}
			docAuthService.updateByUserAuth(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
		
		/**
		 * 查看一个用户在一个组中的授权
		 * @param companyCode
		 * @param username
		 * @param groupId
		 */
		
		@RequestMapping(value="/auth/detailByUser")
		public @ResponseBody ResultBean detailByUser(@RequestBody DocGroupUserAuthFormBean bean){
			ResultBean result = new ResultBean(true,null);
			try {
				if(!StringUtil.isNullOrBlank(bean.getId())){
				bean.setGroupId(Integer.parseInt(bean.getId().split("_")[1]));}
				result.setObject(docAuthService.detailByUserAuth(bean));
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
				if (e instanceof ServiceException) {
					result.setErrorCode(e.getMessage());
				} else {
					result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
				}
			}
			return result;
		}
	
}
